package com.eedi.framework.permission.dal.mapper;

import cn.hutool.core.util.StrUtil;
import com.eedi.framework.common.enums.CommonStatusEnum;
import com.eedi.framework.common.enums.UserTypeEnum;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.mybatis.core.dataobject.BaseDO;
import com.eedi.framework.mybatis.core.mapper.BaseMapperX;
import com.eedi.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.eedi.framework.permission.controller.org.vo.role.OrgRolePageReq;
import com.eedi.framework.permission.controller.org.vo.role.OrgRoleSimpleResp;
import com.eedi.framework.permission.convert.OrgRoleConvert;
import com.eedi.framework.permission.dal.dataobject.OrgRoleDO;
import com.eedi.framework.permission.enums.OrgRoleCodeEnum;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.lang.Nullable;

import java.util.Collection;
import java.util.List;

@Mapper
public interface OrgRoleMapper extends BaseMapperX<OrgRoleDO> {

    default PageResult<OrgRoleDO> selectPage(OrgRolePageReq reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<OrgRoleDO>()
                .likeIfPresent(OrgRoleDO::getOrgRoleName, reqVO.getOrgRoleName())
                .likeIfPresent(OrgRoleDO::getOrgRoleCode, reqVO.getOrgRoleCode())
                .eqIfPresent(OrgRoleDO::getOrgRoleStatus, reqVO.getOrgRoleStatus())
                .betweenIfPresent(BaseDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(OrgRoleDO::getId));
    }

    default OrgRoleDO selectByName(String name, UserTypeEnum orgUserType) {
        LambdaQueryWrapperX<OrgRoleDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(OrgRoleDO::getOrgRoleName, name);
        return selectOne(queryWrapper);
    }
    default OrgRoleDO selectByNameAndOrgInfoId(String orgInfoId,String name, UserTypeEnum orgtemUserType) {
        LambdaQueryWrapperX<OrgRoleDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(OrgRoleDO::getOrgRoleName, name)
                    .eqIfPresent(OrgRoleDO::getOrgInfoId, orgInfoId);
        return selectOne(queryWrapper);
    }

     default List<OrgRoleDO> listByRoleNames(Collection<String> orgRoleNames){
         LambdaQueryWrapperX<OrgRoleDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(OrgRoleDO::getOrgRoleStatus,CommonStatusEnum.ENABLE)
                .in(OrgRoleDO::getOrgRoleName,orgRoleNames);

        return selectList(queryWrapper);
     }

    default OrgRoleDO selectByCode(String code, UserTypeEnum orgtemUserType) {
        LambdaQueryWrapperX<OrgRoleDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(OrgRoleDO::getOrgRoleCode, code);
        return selectOne(queryWrapper);
    }
    default OrgRoleDO selectByCodeAndOrgInfoId(String orgInfoId,String code, UserTypeEnum orgtemUserType) {
        LambdaQueryWrapperX<OrgRoleDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(OrgRoleDO::getOrgRoleCode, code)
                    .eqIfPresent(OrgRoleDO::getOrgInfoId, orgInfoId);
        return selectOne(queryWrapper);
    }

    default List<OrgRoleDO> selectListByStatus(@Nullable Collection<CommonStatusEnum> statuses) {
        LambdaQueryWrapperX<OrgRoleDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.inIfPresent(OrgRoleDO::getOrgRoleStatus, statuses);
        return selectList(queryWrapper);
    }
    default Long countRoleCode(String sysRoleCode){
        if (StrUtil.isBlank(sysRoleCode)){
            return 0L;
        }
        return selectCount(OrgRoleDO::getOrgRoleCode,sysRoleCode);
    }

    default List<OrgRoleSimpleResp> selectByRoleIds(Collection<String> roleIds){
        List<OrgRoleDO> sysRoles = selectBatchIds(roleIds);
        return sysRoles.stream().map(OrgRoleConvert.INSTANCE::convert).toList();
    }

    default List<OrgRoleDO> getRoleListByOrgInfoIdAndRoleCode(String orgInfoId, OrgRoleCodeEnum orgRoleCode){
        LambdaQueryWrapperX<OrgRoleDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.inIfPresent(OrgRoleDO::getOrgRoleStatus, CommonStatusEnum.ENABLE);
        queryWrapper.eqIfPresent(OrgRoleDO::getOrgInfoId, orgInfoId);
        queryWrapper.eqIfPresent(OrgRoleDO::getOrgRoleCode, orgRoleCode);
        return selectList(queryWrapper);
    }

    default List<OrgRoleSimpleResp> selectSysRoleCode(OrgRoleCodeEnum sysRoleCode){
        List<OrgRoleDO> sysRoles = selectList(OrgRoleDO::getOrgRoleCode, sysRoleCode.getCode());
        return sysRoles.stream().map(OrgRoleConvert.INSTANCE::convert).toList();
    }
}
